# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.adminMenuDao import AdminMenuDao

from . import app

CORS(app, supports_credentials=True)
__author__ = "Prince_CHEN"


@app.route('/insertAdminMenu', methods=['POST'])
def insert_admin_menuNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'menuParentId': request.json.get('menuParentId', ''),
        'menuType': request.json.get('menuType', ''),
        'menuTitle': request.json.get('menuTitle', ''),
        'menuIcoUrl': request.json.get('menuIcoUrl', ''),
        'menuLinkUrl': request.json.get('menuLinkUrl', ''),
        'menuSort': request.json.get('menuSort', 99),
        'isLock': request.json.get('isLock', 0),
        'remark': request.json.get('remark', ''),
        'actionKey': request.json.get('actionKey', ''),
        'isSys': request.json.get('isSys', 0)
    }

    info = AdminMenuDao().insertAdminMenu(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findAdminMenuById', methods=['POST'])
def find_admin_menu_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    menuId = request.json.get('menuId', '')
    info = AdminMenuDao().findMenuById(menuId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 1, "status": "error"}})


@app.route('/findAdminMenuByCondition', methods=['POST'])
def find_admin_menu_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = AdminMenuDao().findMenuByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                     multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 0, "status": "error"}})


@app.route('/deleteAdminMenuByIds', methods=['POST'])
def delete_admin_menu_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = AdminMenuDao().deleteAdminMenu(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateAdminMenuById', methods=['POST'])
def update_admin_menu_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        menuId = request.json['menuId']
    except:
        return jsonify({"message": {"code": 1, "status": "请传入要修改信息的ID"}})
    oldInfo = AdminMenuDao().selectAdminMenu(menuId)
    info = {
        'menuId': request.json['menuId'],
        'menuParentId': request.json.get('menuParentId', oldInfo[1]),
        'menuType': request.json.get('menuType', oldInfo[2]),
        'menuTitle': request.json.get('menuTitle', oldInfo[3]),
        'menuIcoUrl': request.json.get('menuIcoUrl', oldInfo[4]),
        'menuLinkUrl': request.json.get('menuLinkUrl', oldInfo[5]),
        'menuSort': request.json.get('menuSort', oldInfo[6]),
        'isLock': request.json.get('isLock', oldInfo[7]),
        'remark': request.json.get('remark', oldInfo[8]),
        'actionKey': request.json.get('actionKey', oldInfo[9]),
        'isSys': request.json.get('isSys', oldInfo[10])
    }

    info = AdminMenuDao().updateAdminMenu(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 1, "status": "error"}})


@app.route('/findMenuByActionkey', methods=['POST'])
def find_by_actionkeyNoToken():
    if not request.json:
        abort(400)
    try:
        actionKey = request.json['actionKey']
        pageIndex = request.json['pageIndex']
        pageSize = request.json['pageSize']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定参数"}})

    info, total = AdminMenuDao().find_by_actionkey(actionKey=actionKey, pageIndex=pageIndex, pageSize=pageSize)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
